﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Common;

using STMS.BLL;
using STMS.Models.DModels;
using STMS.Models.FMOdels;
using STMS.Models.VModels;
using STMS.STMSApp.FModels;
using STMS.STMSApp.util;

namespace STMS.STMSApp.store
{
    public partial class StoreRegionForm : Form
    {
        public StoreRegionForm()
        {
            InitializeComponent();
        }

        StoreRegionBLL storeRegionBll = new StoreRegionBLL();
        private void StoreRegionForm_Load(object sender, EventArgs e)
        {
            checkBox_del.Checked = false;
            txt_keywords.Clear();
            //初始加载仓库下拉框
            FormUtil.LoadCboStores(cbo_stores);
            //初始加载温度状态下拉框
            InitCboStates();
            FindSRegionList();//条件查询仓库分区数据
        }

        private void InitCboStates()
        {
            List<RegionStateModel> stateList = FormUtil.RegionStateList();
            cbo_states.DisplayMember = "StateText";
            cbo_states.ValueMember = "RegionState";
            cbo_states.DataSource = stateList;
            cbo_states.SelectedIndex = 0;
        }


        /// <summary>
        /// 条件查询仓库分区数据
        /// </summary>
        /// <returns></returns>
        private List<ViewStoreRegionInfo> FindSRegionList()
        {
            int storeId = cbo_stores.SelectedValue.GetInt();
            int stateId = cbo_states.SelectedValue.GetInt();
            string keywords = txt_keywords.Text.Trim();
            bool isDeleted = checkBox_del.Checked;
            
            dgv_sRegionList.ShowDgvCols(isDeleted);
            List<ViewStoreRegionInfo> sRegionViewList = storeRegionBll.FindStoreRegion(storeId, stateId, keywords, isDeleted);
     
            if (sRegionViewList.Count > 0)
            {
                SetEnableBtn(true);
                dgv_sRegionList.AutoGenerateColumns = false;
                dgv_sRegionList.DataSource = sRegionViewList;
            }
            else
            {
                SetEnableBtn(false);
                dgv_sRegionList.DataSource = null;

            }
            return sRegionViewList;
        }

        private void SetEnableBtn(bool showDel)
        {
            ucb_del.Enabled = showDel;
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            FindSRegionList();
        }

        private void dgv_sRegionList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var cell = dgv_sRegionList.Rows[e.RowIndex].Cells[e.ColumnIndex];//当前点击的单元格
            string headText = cell.FormattedValue.ToString();
            ViewStoreRegionInfo region = dgv_sRegionList.Rows[e.RowIndex].DataBoundItem as ViewStoreRegionInfo;
            switch (headText)
            {
                case "修改":
                    //打开仓库信息页
                    ShowStoreRegionInfoForm(2,region.SRegionId);
                    break;
                case "删除":
                    //执行逻辑删除（假删除）update
                    DeleteStoreRegion(region, 1);
                    break;
                case "恢复":
                    //恢复（逻辑恢复）
                    DeleteStoreRegion(region, 0);
                    break;
                case "移除":
                    //真删除-delete
                    DeleteStoreRegion(region, 2);
                    break;
            }
        }

        /// <summary>
        /// 仓库分区删除、恢复、真删除
        /// </summary>
        /// <param name="region"></param>
        /// <param name="isDeleted"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DeleteStoreRegion(ViewStoreRegionInfo region, int delCode)
        {
            //string InfoName = "仓库信息";
            string delName = FormUtil.GetDelName(delCode);

            DialogResult result = MessageBox.Show($"{delName}仓库信息，请谨慎操作！", "提示", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                bool isDel = false;
                switch (delCode)
                {
                    case 1:
                        //逻辑删除
                        int reDel = storeRegionBll.LogicDeleteStoreRegion(region.SRegionId,region.StoreId);
                        if (reDel == 1)
                        {
                            //删除成功
                            isDel = true;
                        }
                        else if (reDel == 2)
                        {
                            //该仓库分区 存在产品
                            MessageBox.Show("该仓库分区存在产品，不能删除！");
                            return;
                        }
                        else
                        {
                            //MessageBox.Show($"返回值：{reDel}");
                            isDel = false;
                        }
                        break;

                    case 0:
                        //恢复
                        isDel = storeRegionBll.RecoverStoreRegion(region.SRegionId, region.StoreId);
                        break;
                    case 2:
                        //真删除
                        isDel = storeRegionBll.DeleteStoreRegion(region.SRegionId, region.StoreId);
                        break;
                }

                if (isDel)
                {
                    MessageBox.Show($"{delName}仓库分区信息成功！");
                    FindSRegionList();
                }
                else
                    MessageBox.Show($"{delName}仓库分区信息失败！");
            }
        }

        /// <summary>
        /// 打开仓库分区信息页
        /// </summary>
        /// <param name="actType"></param>
        /// <param name="sRegionId"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ShowStoreRegionInfoForm(int actType, int sRegionId)
        {
            StoreRegionInfoForm storeRegionInfoForm = new StoreRegionInfoForm();
            storeRegionInfoForm.Tag = new FInfoData()
            {
                ActType = actType,
                FId = sRegionId
            };
            //刷新列表页数据效果  委托，采用事件
            storeRegionInfoForm.ReLoadStoreRegionList += () => FindSRegionList();
            storeRegionInfoForm.StartPosition = FormStartPosition.CenterScreen;
            storeRegionInfoForm.ShowDialog();
        }

        /// <summary>
        /// 新增仓库分区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_add_Click(object sender, EventArgs e)
        {
            ShowStoreRegionInfoForm(1, 0);
        }
    }
}
